package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class HttpClient {
	
	public static void main(String[] args) {
		String urlString = "http://127.0.0.1:8080/httpServer/servlet/SP_HTTP_H2D_InvalidBarcode?name=你";
		doGet(urlString);
	}
	

	public static String doGet(String HttpUrl){
		HttpURLConnection connection = null;
		InputStream is = null;
		BufferedReader br = null;
		String result = null;
		
		try {
			//创建远程URL连接对象
			URL url = new URL(HttpUrl);
			//通过URL对象打开一个连接
			connection = (HttpURLConnection)url.openConnection();
			//设置请求方式
			connection.setRequestMethod("GET");
			//设置连接主机服务器的超时时间
			connection.setConnectTimeout(15000);
			//设置读取远程返回数据时间
			connection.setReadTimeout(60000);
			//发送请求
			connection.connect();
			System.out.println(connection.getResponseCode());
			if(connection.getResponseCode() == 200){
				//通过connection获取输入流
				 is = connection.getInputStream();
				//封装输入流，并设置编码
				 br = new BufferedReader(new InputStreamReader(is, "utf-8"));
				StringBuffer sbf = new StringBuffer();
				String temp = null;
				while((temp = br.readLine())!=null){
					sbf.append(temp);
					sbf.append("\r\n");
				}
				result = sbf.toString();
				System.out.println("结果："+result);
			}
			if(connection.getResponseCode() == 201){
				System.out.println("201");
				
			}
		} catch (MalformedURLException e) {
			System.out.println("11");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("22");
			e.printStackTrace();
		}finally{
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			connection.disconnect();
		}
		return result;
	}
	
	public static String doPost(String url,String query) throws IOException {

        URL restURL = new URL(url);
        /*
         * 此处的urlConnection对象实际上是根据URL的请求协议(此处是http)生成的URLConnection类 的子类HttpURLConnection
         */
        HttpURLConnection conn = (HttpURLConnection) restURL.openConnection();
        //请求方式
        conn.setRequestMethod("POST");
        //设置是否从httpUrlConnection读入，默认情况下是true; httpUrlConnection.setDoInput(true);
        conn.setDoOutput(true);
        //allowUserInteraction 如果为 true，则在允许用户交互（例如弹出一个验证对话框）的上下文中对此 URL 进行检查。
        conn.setAllowUserInteraction(false);

        PrintStream ps = new PrintStream(conn.getOutputStream());
        ps.print(query);

        ps.close();

        BufferedReader bReader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

        String line,resultStr="";

        while(null != (line=bReader.readLine()))
        {
        resultStr +=line;
        }
        System.out.println("3412412---"+resultStr);
        bReader.close();

        return resultStr;

    }
    

	
	
}